package zhtt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhtt.entity.Expression;
import zhtt.scheduler.ZhttScheduler;

import java.net.URLEncoder;
import java.util.Date;

/**
 * Created by zhtt on 2016/10/29.
 */
@RestController
//@RequestMapping("/expression")
public class ExpressionController {

    @Autowired private ZhttScheduler scheduler;

    //$.post("http://localhost:8080/ssd/api/save?expression="+encodeURIComponent("*/10 * * * * ?")+"&remarks="+encodeURIComponent("1天执行一次"));
    @ResponseBody
    @RequestMapping("/reset")
    private String reset(@RequestParam("expression")String expression,@RequestParam("remarks")String remarks){
        try{
            if(expression!=null&&expression.length()>0){
                Expression expres=new Expression(expression,true);
                expres.setRemarks(remarks);
                scheduler.reStart(new Date(),expres);
            }
            return "成功";
        }catch (Exception e){
            return e.getMessage();
        }
    }


    //$.post("http://localhost:8080/ssd/api/save?expression="+encodeURIComponent("*/10 * * * * ?"));
    @ResponseBody
    @RequestMapping(value = "/save",produces = "text/html;charset=UTF-8")/** 解决中文乱码  **/
    private String save(@RequestParam("expression")String expression){
        try{
            String url1 = new String(expression.getBytes("UTF-8"), "UTF-8");
            String url2 = URLEncoder.encode(expression, "UTF-8");
            return "成功【"+expression+"】【"+url1+"】【"+url2+"】";
        }catch (Exception e){
            return e.getMessage();
        }
    }
    @ResponseBody
    @RequestMapping(value = "/stop",produces = "text/html;charset=UTF-8")/** 解决中文乱码  **/
    private String stop(){
        try{
            scheduler.stop();
            return "成功";
        }catch (Exception e){
            return e.getMessage();
        }
    }
}
